package com.caidan.comsumer.config;

import jakarta.annotation.Resource;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @项目名 rabbit-mq-learning
 * @描述
 * @作者 wzy
 * @时间 2024/5/20 20:08
 */

@Configuration
//条件注解 判断只有说配置文件中书写了enabled 且其值是true的情况下才生效，这个是配置错误信息传递
//给异常队列error.queue
@ConditionalOnProperty(prefix = "spring.rabbitmq.listener.simple.retry",
        name = "enabled",
        havingValue = "true")
public class ErrorConfiguration {
    //    这个位置是怎么注入呢？
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("error.direct");
    }

    @Bean
    public Queue errorQueue() {
        return new Queue("error.queue");
    }

    //    这个方法名可以不一致 注入还是Spring实现的
    @Bean
    public Binding errorBinding(Queue errorQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(errorQueue).to(directExchange).with("error");
    }

    @Bean
    public MessageRecoverer messageRecoverer(RabbitTemplate rabbitTemplate) {
        return new RepublishMessageRecoverer(rabbitTemplate, "error.direct", "error");
    }

}